package com.syezon.wifikey.news;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.syezon.wifikey.R;
import com.syezon.wifikey.base.BaseLazyFragment;
import com.syezon.wifikey.news.adapter.YiYouLiaoNewsAdapter;
import com.syezon.wifikey.news.info.YiYouLiaoNewsInfoBean;
import com.syezon.wifikey.news.viewmodel.YiYouLiaoNewsListViewModel;

import java.util.List;

import butterknife.BindView;

public class YiYouLiaoNewListFragment extends BaseLazyFragment implements OnRefreshLoadMoreListener {
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_news_list)
    RecyclerView mRvNewsList;
    YiYouLiaoNewsListViewModel mNewsViewModel;
    private YiYouLiaoNewsAdapter mAdapter;
    private static final String CHANNEL_TAG = "channelTag";
    private String channelTag = "";

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        mNewsViewModel.loadMore(channelTag);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        mNewsViewModel.onRefresh(YiYouLiaoNewsListViewModel.EVENT_REFRESH, channelTag);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.app_yiyouliao_news_fragment_list;
    }

    @Override
    protected void initViewsAndEvents() {
        mNewsViewModel = ViewModelProviders.of(this).get(YiYouLiaoNewsListViewModel.class);
        mRvNewsList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new YiYouLiaoNewsAdapter(getContext());
        mRvNewsList.setAdapter(mAdapter);
        mRefreshLayout.setOnRefreshLoadMoreListener(this);
        mNewsViewModel.getRefreshMutableLiveData().observe(this, new Observer<List<YiYouLiaoNewsInfoBean>>() {
            @Override
            public void onChanged(@Nullable List<YiYouLiaoNewsInfoBean> yiYouLiaoNewsInfoBeans) {
                if (yiYouLiaoNewsInfoBeans != null && yiYouLiaoNewsInfoBeans.size() > 0) {
                    toggleShowLoading(false, "");
                    mAdapter.setData(yiYouLiaoNewsInfoBeans);
                    mRefreshLayout.finishRefresh();
                } else {
                    toggleShowError(true, "数据获取异常!", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            refresh();
                        }
                    });
                }
            }
        });

        mNewsViewModel.getLoadMoreMutableLiveData().observe(this, new Observer<List<YiYouLiaoNewsInfoBean>>() {
            @Override
            public void onChanged(@Nullable List<YiYouLiaoNewsInfoBean> yiYouLiaoNewsInfoBeans) {
                if (yiYouLiaoNewsInfoBeans != null && yiYouLiaoNewsInfoBeans.size() > 0) {
                    mAdapter.addData(yiYouLiaoNewsInfoBeans);
                    mRefreshLayout.finishLoadMore();
                } else {
                    mRefreshLayout.finishLoadMore(false);
                }
            }
        });

    }

    @Override
    protected void onFirstUserVisible() {
        super.onFirstUserVisible();
        LogUtils.e("开始懒加载");
        refresh();
    }

    private void refresh() {
        toggleShowLoading(true, "正在加载...");
        mNewsViewModel.onRefresh(YiYouLiaoNewsListViewModel.EVENT_REFRESH, channelTag);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            channelTag = getArguments().getString(CHANNEL_TAG, "");
        }
    }

    public static YiYouLiaoNewListFragment newInstance(String channelTag) {
        YiYouLiaoNewListFragment fragment = new YiYouLiaoNewListFragment();
        Bundle args = new Bundle();
        args.putString(CHANNEL_TAG, channelTag);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected View getLoadingTargetView() {
        return mRefreshLayout;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
